package com.hcb.micro.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Discuss;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.QqbsAccount;
import com.hcb.micro.dto.UnionConsume;
import com.hcb.micro.service.IUnionConsumeService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("unionConsume")
public class UnionConsumeController extends BaseControllers{
	@Autowired
	IUnionConsumeService unionConsumeService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String listByType() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);

	Map<String,Object> para = new HashMap<>();
	if (bodyInfo.get("unionId") != null && !"".equals(bodyInfo.get("unionId"))) {
		para.put("unionId", bodyInfo.getString("unionId"));
	}
	if(bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
		para.put("accountId", bodyInfo.getString("accountId"));
	}
	if(bodyInfo.get("useAccumulatePoints") != null && !"".equals(bodyInfo.get("useAccumulatePoints"))) {
		para.put("useAccumulatePoints", bodyInfo.getString("useAccumulatePoints"));
	}
	
	if(bodyInfo.get("useMoney") != null && !"".equals(bodyInfo.get("useMoney"))) {
		para.put("useMoney", bodyInfo.getString("useMoney"));
	}

	if(bodyInfo.get("total") != null && !"".equals(bodyInfo.get("total"))) {
		para.put("total", bodyInfo.getString("total"));
	}

	
	List<UnionConsume> dListAll = unionConsumeService.selectByPara(para);
	
	if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
		para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
	}
	if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
		para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
	}
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	List<UnionConsume> dList = unionConsumeService.selectByPara(para);
	
	if(dList.size() <= 0){
		json.put("result", "1");
		json.put("description", "消费列表不存在");
		return buildReqJsonObject(json);
	}
	List<Map<String,Object>> list = new ArrayList<>();
	for(UnionConsume d:dList){
		
		Map<String,Object> map = new HashMap<>();
		map.put("id", d.getId());
		map.put("unionId", d.getUnionId());
		map.put("accountId", d.getAccountId());
		map.put("useAccumulatePoints", d.getUseAccumulatePoints());
		map.put("useMoney", d.getUseMoney());
		map.put("total", d.getTotal());
		map.put("createTime", sdf.format(d.getCreateTime()));
		list.add(map);
	}
	
	json.put("result", "0");
	json.put("description", "获取消费列表成功");
	json.put("list", list);
	json.put("count", dListAll.size());
	return buildReqJsonObject(json);
}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Date date = new Date();
		
		UnionConsume d = unionConsumeService.selectByPrimaryKey(bodyInfo.getInt("id"));
		
		if(d == null){
			json.put("result", "1");
			json.put("description", "不存在");
			return buildReqJsonObject(json);
		}
			
		
		if(bodyInfo.get("unionId") != null && !"".equals(bodyInfo.get("unionId"))) {
			d.setUnionId(bodyInfo.getInt("unionId"));
		}
		
		if(bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
			d.setAccountId(bodyInfo.getInt("accountId"));
		}
		
		if(bodyInfo.get("contuseAccumulatePointsent") != null && !"".equals(bodyInfo.get("useAccumulatePoints"))) {
			d.setUseAccumulatePoints( (float) bodyInfo.getInt("contuseAccumulatePointsent"));
		}
		
		if(bodyInfo.get("status") != null && !"".equals(bodyInfo.get("useMoney"))) {
			d.setUseMoney((float) bodyInfo.getInt("useMoney"));
		}
		if(bodyInfo.get("total") != null && !"".equals(bodyInfo.get("total"))) {
			d.setTotal((float) bodyInfo.getInt("total"));
		}
		
		
		
		d.setCreateTime(date);
		
		Integer update  = unionConsumeService.updateByPrimaryKeySelective(d);
		if(update == 0){
			json.put("result", "1");
			json.put("description", "修改失败");
			return buildReqJsonObject(json);
		}
		
		json.put("result", "0");
		json.put("description", "修改成功");
		json.put("discussId", d.getId());
		return buildReqJsonObject(json);
	}
}
